package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增菜品和对应的口味
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void insert(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //添加菜品进入数据库,主键返回添加菜品对应的口味时使用
        dishMapper.insertDish(dish);
        Long dishId = dish.getId();//主键赋值给dish的id属性

        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断是否传递了菜品口味
        if (flavors.size() == 0) {
            return;
        }

        //添加菜品对应的口味
        flavors.forEach(flavor -> {
            flavor.setDishId(dishId);
        });
        dishMapper.insertFlavor(flavors);

    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageList(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());//开始分页
        Page<DishVO> page = dishMapper.pageList(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     */
    @Override
    public DishVO findById(Long id) {
        //根据id查询菜品
        Dish dish = dishMapper.findById(id);

        //根据id查询菜品口味
        List<DishFlavor> flavors = dishMapper.findFlavors(dish.getId());

        //封装数据返回对象
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);

        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //编辑菜品
        dishMapper.update(dish);

        //编辑菜品对应的口味
        //先删除当前菜品对应的所有口味
        ArrayList<Long> id = new ArrayList<>();
        id.add(dish.getId());
        dishMapper.deleteFlavor(id);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断是否传递了菜品口味
        if (flavors.size() == 0) {
            return;
        }
        //重新添加菜品对应的口味
        flavors.forEach(flavor->{flavor.setDishId(dish.getId());});
        dishMapper.insertFlavor(flavors);
    }

    @Autowired
    private SetMealMapper setMealMapper;
    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional//保证事务的一致性
    public void deleteByIds(List<Long> ids) {
        //起售中的菜单不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.findById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                //如果菜品是起售状态
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);//商品起售中异常
            }
        }

        //被套餐关联的菜品不能删除
        for (Long id : ids) {
            Integer count = setMealMapper.countAboutDish(id);
            if(count > 0){
                //菜品关联套餐
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

        //删除菜品
        dishMapper.deleteByIds(ids);

        //删除菜品后对应的口味也要删除
        dishMapper.deleteFlavor(ids);
    }

    /**
     * 菜品的状态
     * @param status
     */
    @Override
    public void status(Integer status,Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> findList(Long categoryId) {
        return dishMapper.findList(categoryId);
    }

    /**
     * 根据分类id查询菜品和对应的口味
     */
    @Override
    public List<DishVO> list(Long categoryId){
        List<DishVO> dishVOS = new ArrayList<>();
        List<Dish> list = dishMapper.findList(categoryId);
        for (Dish dish : list) {
            Long dishId = dish.getId();
            List<DishFlavor> dishFlavors = dishMapper.findFlavors(dishId);
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            dishVO.setFlavors(dishFlavors);
            dishVOS.add(dishVO);
        }
        return dishVOS;
    }
}









